package com.example.pet.controller;


import com.hj.pet.api.PetShowService;
import com.hj.pet.model.PetInfo;
import com.hj.pet.model.QueryCondition;
import com.hj.pet.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.hj.pet.constant.Code.FAIL_NULL;
import static com.hj.pet.constant.Code.SUCCESS;

@RequestMapping("/internal/show")
@RestController
public class PetShopController {
    @Autowired
    PetShowService petShowService;

    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public ReturnT<?> save(@RequestBody PetInfo petInfo){
        petShowService.save(petInfo);
        return new ReturnT<>(SUCCESS, "success");
    }

    @RequestMapping(value="updateById",method = RequestMethod.POST)
    @ResponseBody
    public ReturnT<?> updateById(@RequestBody PetInfo petInfo){
        petShowService.updateById(petInfo);
        return new ReturnT<>(SUCCESS, "success");
    }

    @RequestMapping(value="findPetById",method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<?> findPetById(@RequestParam int petId) {
        PetInfo petInfo=petShowService.findPetById(petId);
        return new ReturnT<>(SUCCESS,petInfo);
    }

    @RequestMapping(value = "findPetsByType",method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<?> findPetsByType(@RequestParam String type) {
        List<PetInfo> petInfos=petShowService.findPetsByType(type);
        return new ReturnT<>(SUCCESS,petInfos);
    }

    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<?> findAll(){
        List<PetInfo> petInfos=petShowService.findAll();
        return new ReturnT<>(SUCCESS,petInfos);
    }

    @RequestMapping(value = "findPetsByPrice",method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<?> findPetsByPrice(Integer price1, Integer price2) {
        List<PetInfo> petInfos=petShowService.findPetsByPrice(price1,price2);
        return new ReturnT<>(SUCCESS,petInfos);
    }

    @RequestMapping(value = "findPetsByUserAccount",method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<?> findPetsByUserAccount(@RequestParam String user_account) {
        List<PetInfo> petInfos=petShowService.findPetsByUserAccount(user_account);
        return new ReturnT<>(SUCCESS,petInfos);
    }

    @RequestMapping(value = "findPetsByPetName",method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<?> findPetsByPetName(@RequestParam String pet_name) {
        List<PetInfo> petInfos=petShowService.findPetsByPetName(pet_name);
        return new ReturnT<>(SUCCESS,petInfos);
    }

    @RequestMapping(value = "delPetById",method = RequestMethod.DELETE)
    @ResponseBody
    public ReturnT<?> delPetById(@RequestParam int petId){
        petShowService.delPetById(petId);
        return new ReturnT<>(SUCCESS,"success");
    }

    @RequestMapping(value = "query", method = RequestMethod.POST)
    @ResponseBody
    public ReturnT<?> showList(@RequestBody QueryCondition condition) {
        List<PetInfo> pets = petShowService.searchByQuery(condition);
        if (CollectionUtils.isEmpty(pets)) {
            return new ReturnT<>(FAIL_NULL, "未查到相关宠物信息，请更新查询条件");
        }
        return new ReturnT<>(SUCCESS, pets);
    }

    @RequestMapping(value = "addPet",method = RequestMethod.POST)
    @ResponseBody
    public ReturnT<?> addPet(PetInfo petInfo){
        petShowService.addPet(petInfo);
        return new ReturnT<>(SUCCESS,"success");
    }
}
